home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GDEVPJET.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  9KB  |  253 lines

  1. /* Copyright (C) 1991, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevpjet.c */
  20. /* H-P PaintJet, PaintJet XL, and DEC LJ250 driver for Ghostscript. */
  21. /* Thanks to Rob Reiss (rob@moray.berkeley.edu) for the PaintJet XL */
  22. /* modifications. */
  23. #include "gdevprn.h"
  24. #include "gdevpcl.h"
  25.  
  26. /* X_DPI and Y_DPI must be the same, and may be either 90 or 180. */
  27. #define X_DPI 180
  28. #define Y_DPI 180
  29.  
  30. /* We round up LINE_SIZE to a multiple of 8 bytes */
  31. /* because that's the unit of transposition from pixels to planes. */
  32. #define LINE_SIZE ((X_DPI * 85 / 10 + 63) / 64 * 8)
  33.  
  34. /* The device descriptors */
  35. private dev_proc_print_page(lj250_print_page);
  36. private dev_proc_print_page(paintjet_print_page);
  37. private dev_proc_print_page(pjetxl_print_page);
  38. private int pj_common_print_page(P4(gx_device_printer *, FILE *, int, const char *));
  39. private gx_device_procs paintjet_procs =
  40.   prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  41.     gdev_pcl_3bit_map_rgb_color, gdev_pcl_3bit_map_color_rgb);
  42. gx_device_printer far_data gs_lj250_device =
  43.   prn_device(paintjet_procs, "lj250",
  44.     85,                /* width_10ths, 8.5" */
  45.     110,                /* height_10ths, 11" */
  46.     X_DPI, Y_DPI,
  47.     0.25, 0, 0.25, 0,        /* margins */
  48.     3, lj250_print_page);
  49. gx_device_printer far_data gs_paintjet_device =
  50.   prn_device(paintjet_procs, "paintjet",
  51.     85,                /* width_10ths, 8.5" */
  52.     110,                /* height_10ths, 11" */
  53.     X_DPI, Y_DPI,
  54.     0.25, 0, 0.25, 0,        /* margins */
  55.     3, paintjet_print_page);
  56. private gx_device_procs pjetxl_procs =
  57.   prn_color_matrix_procs(gdev_prn_open, gdev_pcl_get_initial_matrix,
  58.     gdev_prn_output_page, gdev_prn_close,
  59.     gdev_pcl_3bit_map_rgb_color, gdev_pcl_3bit_map_color_rgb);
  60. gx_device_printer far_data gs_pjetxl_device =
  61.   prn_device(pjetxl_procs, "pjetxl",
  62.     85,                /* width_10ths, 8.5" */
  63.     110,                /* height_10ths, 11" */
  64.     X_DPI, Y_DPI,
  65.     0.25, 0, 0, 0,            /* margins */
  66.     3, pjetxl_print_page);
  67.  
  68. /* Forward references */
  69. private int compress1_row(P3(const byte *, const byte *, byte *));
  70.  
  71. /* ------ Internal routines ------ */
  72.  
  73. /* Send a page to the LJ250.  We need to enter and exit */
  74. /* the PaintJet emulation mode. */
  75. private int
  76. lj250_print_page(gx_device_printer *pdev, FILE *prn_stream)
  77. {    fputs("\033%8", prn_stream);    /* Enter PCL emulation mode */
  78.     /* ends raster graphics to set raster graphics resolution */
  79.     fputs("\033*rB", prn_stream);
  80.     /* Exit PCL emulation mode after printing */
  81.     return pj_common_print_page(pdev, prn_stream, 0, "\033*r0B\014\033%@");
  82. }
  83.  
  84. /* Send a page to the PaintJet. */
  85. private int
  86. paintjet_print_page(gx_device_printer *pdev, FILE *prn_stream)
  87. {    /* ends raster graphics to set raster graphics resolution */
  88.     fputs("\033*rB", prn_stream);
  89.     return pj_common_print_page(pdev, prn_stream, 0, "\033*r0B\014");
  90. }
  91.  
  92. /* Send a page to the PaintJet XL. */
  93. private int
  94. pjetxl_print_page(gx_device_printer *pdev, FILE *prn_stream)
  95. {    /* Initialize PaintJet XL for printing */
  96.     fputs("\033E", prn_stream);
  97.     /* The XL has a different vertical origin, who knows why?? */
  98.     return pj_common_print_page(pdev, prn_stream, -360, "\033*rC");
  99. }
  100.  
  101. /* Send the page to the printer.  Compress each scan line. */
  102. private int
  103. pj_common_print_page(gx_device_printer *pdev, FILE *prn_stream, int y_origin,
  104.   const char *end_page)
  105. {
  106. #define DATA_SIZE (LINE_SIZE * 8)
  107.     byte *data =
  108.         (byte *)gs_malloc(DATA_SIZE, 1,
  109.                   "paintjet_print_page(data)");
  110.     byte *plane_data =
  111.         (byte *)gs_malloc(LINE_SIZE * 3, 1,
  112.                   "paintjet_print_page(plane_data)");
  113.     if ( data == 0 || plane_data == 0 )
  114.     {    if ( data )
  115.             gs_free((char *)data, DATA_SIZE, 1,
  116.                 "paintjet_print_page(data)");
  117.         if ( plane_data )
  118.             gs_free((char *)plane_data, LINE_SIZE * 3, 1,
  119.                 "paintjet_print_page(plane_data)");
  120.         return_error(gs_error_VMerror);
  121.     }
  122.  
  123.     /* set raster graphics resolution -- 90 or 180 dpi */
  124.     fprintf(prn_stream, "\033*t%dR", X_DPI);
  125.  
  126.     /* set the line width */
  127.     fprintf(prn_stream, "\033*r%dS", DATA_SIZE);
  128.  
  129.     /* set the number of color planes */
  130.     fprintf(prn_stream, "\033*r%dU", 3);        /* always 3 */
  131.  
  132.     /* move to top left of page */
  133.     fprintf(prn_stream, "\033&a0H\033&a%dV", y_origin);
  134.  
  135.     /* select data compression */
  136.     fputs("\033*b1M", prn_stream);
  137.  
  138.     /* start raster graphics */
  139.     fputs("\033*r1A", prn_stream);
  140.  
  141.     /* Send each scan line in turn */
  142.        {    int lnum;
  143.         int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  144.         int num_blank_lines = 0;
  145.         for ( lnum = 0; lnum < pdev->height; lnum++ )
  146.            {    byte *end_data = data + line_size;
  147.             gdev_prn_copy_scan_lines(pdev, lnum,
  148.                          (byte *)data, line_size);
  149.             /* Remove trailing 0s. */
  150.             while ( end_data > data && end_data[-1] == 0 )
  151.                 end_data--;
  152.             if ( end_data == data )
  153.                {    /* Blank line */
  154.                 num_blank_lines++;
  155.                }
  156.             else
  157.                {    int i;
  158.                 byte *odp;
  159.                 byte *row;
  160.  
  161.                 /* Pad with 0s to fill out the last */
  162.                 /* block of 8 bytes. */
  163.                 memset(end_data, 0, 7);
  164.  
  165.                 /* Transpose the data to get pixel planes. */
  166.                 for ( i = 0, odp = plane_data; i < DATA_SIZE;
  167.                       i += 8, odp++
  168.                     )
  169.                  { /* The following is for 16-bit machines */
  170. #define spread3(c)\
  171.  { 0, c, c*0x100, c*0x101, c*0x10000L, c*0x10001L, c*0x10100L, c*0x10101L }
  172.                    static ulong spr40[8] = spread3(0x40);
  173.                    static ulong spr8[8] = spread3(8);
  174.                    static ulong spr2[8] = spread3(2);
  175.                    register byte *dp = data + i;
  176.                    register ulong pword =
  177.                      (spr40[dp[0]] << 1) +
  178.                      (spr40[dp[1]]) +
  179.                      (spr40[dp[2]] >> 1) +
  180.                      (spr8[dp[3]] << 1) +
  181.                      (spr8[dp[4]]) +
  182.                      (spr8[dp[5]] >> 1) +
  183.                      (spr2[dp[6]]) +
  184.                      (spr2[dp[7]] >> 1);
  185.                    odp[0] = (byte)(pword >> 16);
  186.                    odp[LINE_SIZE] = (byte)(pword >> 8);
  187.                    odp[LINE_SIZE*2] = (byte)(pword);
  188.                  }
  189.                 /* Skip blank lines if any */
  190.                 if ( num_blank_lines > 0 )
  191.                    {    /* move down from current position */
  192.                     fprintf(prn_stream, "\033&a+%dV",
  193.                         num_blank_lines * (720 / Y_DPI));
  194.                     num_blank_lines = 0;
  195.                    }
  196.  
  197.                 /* Transfer raster graphics */
  198.                 /* in the order R, G, B. */
  199.                 for ( row = plane_data + LINE_SIZE * 2, i = 0;
  200.                       i < 3; row -= LINE_SIZE, i++
  201.                     )
  202.                    {    byte temp[LINE_SIZE * 2];
  203.                     int count = compress1_row(row, row + LINE_SIZE, temp);
  204.                     fprintf(prn_stream, "\033*b%d%c",
  205.                         count, "VVW"[i]);
  206.                     fwrite(temp, sizeof(byte),
  207.                            count, prn_stream);
  208.                    }
  209.                }
  210.            }
  211.        }
  212.  
  213.     /* end the page */
  214.     fputs(end_page, prn_stream);
  215.  
  216.     gs_free((char *)data, DATA_SIZE, 1, "paintjet_print_page(data)");
  217.     gs_free((char *)plane_data, LINE_SIZE * 3, 1, "paintjet_print_page(plane_data)");
  218.  
  219.     return 0;
  220. }
  221.  
  222. /*
  223.  * Row compression for the H-P PaintJet.
  224.  * Compresses data from row up to end_row, storing the result
  225.  * starting at compressed.  Returns the number of bytes stored.
  226.  * The compressed format consists of a byte N followed by a
  227.  * data byte that is to be repeated N+1 times.
  228.  * In the worst case, the `compressed' representation is
  229.  * twice as large as the input.
  230.  * We complement the bytes at the same time, because
  231.  * we accumulated the image in complemented form.
  232.  */
  233. private int
  234. compress1_row(const byte *row, const byte *end_row,
  235.   byte *compressed)
  236. {    register const byte *in = row;
  237.     register byte *out